﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Data;

public partial class _1_7 : System.Web.UI.Page
{


    SqlConnection sqlcon;
    string strCon = WebConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString;
        
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            LabelTiShi.Visible = false;

            sqlcon = new SqlConnection(strCon);
            sqlcon.Open();

            SqlCommand sqlcmd = new SqlCommand("select * from a01107_1 where  DATA_MONTH='" + Session["ShenBaoNianDu"] + "' and qiyeid=" + Session["qiyeid"], sqlcon);
            SqlDataReader reader = sqlcmd.ExecuteReader(CommandBehavior.CloseConnection);
            if (reader.Read())
            {
                TextBoxPFKSL.Text = reader["OUTPUT_NUMBER"].ToString();
                TextBoxWSPFL.Text = reader["YEAR_SUMCOUNT"].ToString();
                TextBoxDBPFL.Text = reader["REACH_CNT"].ToString();
                TextBoxCBPFL.Text = reader["OVER_CNT"].ToString();
                TextBoxZJPRHL.Text = reader["INSEA_COUNT"].ToString();
                TextBoxZJPRJHHKL.Text = reader["INRIVER_COUNT"].ToString();
                TextBoxPRCSGWL.Text = reader["CITY_COUNT"].ToString();
                TextBoxPRCZWSCLCL.Text = reader["INFACT_COUNT"].ToString();
                TextBoxQTQXL.Text = reader["OTHER_OUT"].ToString();
                reader.Close();
            }
            else //未有记录则新增一条
            {
                reader.Close();

                sqlcmd.CommandText = "INSERT INTO a01107_1(qiyeid,DATA_MONTH) VALUES(" + Session["qiyeid"] + ",'" + Session["ShenBaoNianDu"] + "')";
                sqlcon.Open();
                sqlcmd.ExecuteNonQuery();
            }
            sqlcon.Close();


            //**********读取污染物数据***********
            sqlcon.Close();
            sqlcmd.CommandText = "select * from a01107_2 where  DATA_MONTH='" + Session["ShenBaoNianDu"] + "' and qiyeid=" + Session["qiyeid"];
            sqlcon.Open();
            reader = sqlcmd.ExecuteReader(CommandBehavior.CloseConnection);

            HiddenField[] hb = new HiddenField[] { this.HiddenField1, this.HiddenField2, this.HiddenField3, this.HiddenField4, this.HiddenField5, this.HiddenField6, this.HiddenField7, this.HiddenField8, this.HiddenField9, this.HiddenField10, this.HiddenField11, this.HiddenField12, this.HiddenField13, this.HiddenField14, this.HiddenField15, this.HiddenField16 };
            TextBox[] tbxQCL = new TextBox[] { this.TextBoxQCL1, this.TextBoxQCL2, this.TextBoxQCL3, this.TextBoxQCL4, this.TextBoxQCL5, this.TextBoxQCL6, this.TextBoxQCL7, this.TextBoxQCL8, this.TextBoxQCL9, this.TextBoxQCL10, this.TextBoxQCL11, this.TextBoxQCL12, this.TextBoxQCL13, this.TextBoxQCL14, this.TextBoxQCL15, this.TextBoxQCL16 };
            TextBox[] tbxXZQCL = new TextBox[] { this.TextBoxXZQCL1, this.TextBoxQCL2, this.TextBoxXZQCL3, this.TextBoxXZQCL4, this.TextBoxXZQCL5, this.TextBoxXZQCL6, this.TextBoxXZQCL7, this.TextBoxXZQCL8, this.TextBoxXZQCL9, this.TextBoxXZQCL10, this.TextBoxXZQCL11, this.TextBoxXZQCL12, this.TextBoxXZQCL13, this.TextBoxXZQCL14, this.TextBoxXZQCL15, this.TextBoxXZQCL16 };
            TextBox[] tbxHJ = new TextBox[] { this.TextBoxHJ1, this.TextBoxHJ2, this.TextBoxHJ3, this.TextBoxHJ4, this.TextBoxHJ5, this.TextBoxHJ6, this.TextBoxHJ7, this.TextBoxHJ8, this.TextBoxHJ9, this.TextBoxHJ10, this.TextBoxHJ11, this.TextBoxHJ12, this.TextBoxHJ13, this.TextBoxHJ14, this.TextBoxHJ15, this.TextBoxHJ16 };
            TextBox[] tbxDBL = new TextBox[] { this.TextBoxDBL1, this.TextBoxDBL2, this.TextBoxDBL3, this.TextBoxDBL4, this.TextBoxDBL5, this.TextBoxDBL6, this.TextBoxDBL7, this.TextBoxDBL8, this.TextBoxDBL9, this.TextBoxDBL10, this.TextBoxDBL11, this.TextBoxDBL12, this.TextBoxDBL13, this.TextBoxDBL14, this.TextBoxDBL15, this.TextBoxDBL16 };
            TextBox[] tbxCBL = new TextBox[] { this.TextBoxCBL1, this.TextBoxCBL2, this.TextBoxCBL3, this.TextBoxCBL4, this.TextBoxCBL5, this.TextBoxCBL6, this.TextBoxCBL7, this.TextBoxCBL8, this.TextBoxCBL9, this.TextBoxCBL10, this.TextBoxCBL11, this.TextBoxCBL12, this.TextBoxCBL13, this.TextBoxCBL14, this.TextBoxCBL15, this.TextBoxCBL16 };
            DropDownList[] dpl = new DropDownList[] { this.DropDownList12, this.DropDownList13, this.DropDownList14, this.DropDownList15, this.DropDownList16 };

            int xh;
            while (reader.Read())
            {
                xh = Convert.ToInt32(reader["SRKXH"]) - 1;
                hb[xh].Value = "edit";
                if (xh>=11)
                {
                    dpl[xh - 11].SelectedValue = reader["CODE"].ToString();  
                }
                tbxQCL[xh].Text = reader["DEAL_COUNT"].ToString();
                tbxXZQCL[xh].Text = reader["NEW_NUM"].ToString();
                tbxHJ[xh].Text = reader["YEAR_SUMCOUNT"].ToString();
                tbxDBL[xh].Text = reader["REACH_COUNT"].ToString();
                tbxCBL[xh].Text = reader["OVER_COUNT"].ToString();
            }
            reader.Close();
            sqlcon.Close();
        }

    }


    public string DisplayStr()
    {

        YingYongHanShu yyhs = new YingYongHanShu();

        return yyhs.DisplayStr2("101107");
    }



    protected void ButtonTiJiao_Click(object sender, EventArgs e)
    {
        if (Page.IsValid) //当所有校验控件都正确是才提交
        {

            //**********逻辑判断***********
            //应加入判断输入框 3+4=5+6+7+8+9

            LabelTiShi.Text = "正在提交中，请不要关闭或离开...";
            LabelTiShi.Visible = true;

            sqlcon = new SqlConnection(strCon);
            sqlcon.Open();

            ZhuanHuan zh = new ZhuanHuan();//调用转换类

            string sqlstr = "update a01107_1 set "
                + "OUTPUT_NUMBER=" + Convert.ToString(zh.StrToDouble(TextBoxPFKSL.Text.Trim()))
                + ",YEAR_SUMCOUNT=" + (zh.StrToDouble(TextBoxDBPFL.Text.Trim()) + zh.StrToDouble(TextBoxCBPFL.Text.Trim())).ToString("0.00")
                + ",REACH_CNT=" + Convert.ToString(zh.StrToDouble(TextBoxDBPFL.Text.Trim()))
                + ",OVER_CNT=" + Convert.ToString(zh.StrToDouble(TextBoxCBPFL.Text.Trim()))
                + ",INSEA_COUNT=" + Convert.ToString(zh.StrToDouble(TextBoxZJPRHL.Text.Trim()))
                + ",INRIVER_COUNT=" + Convert.ToString(zh.StrToDouble(TextBoxZJPRJHHKL.Text.Trim()))
                + ",CITY_COUNT=" + Convert.ToString(zh.StrToDouble(TextBoxPRCSGWL.Text.Trim()))
                + ",INFACT_COUNT=" + Convert.ToString(zh.StrToDouble(TextBoxPRCZWSCLCL.Text.Trim()))
                + ",OTHER_OUT=" + Convert.ToString(zh.StrToDouble(TextBoxQTQXL.Text.Trim()))
                + " where qiyeid=" + Session["qiyeid"] + " and  DATA_MONTH='" + Session["ShenBaoNianDu"] + "'";
            SqlCommand sqlcmd = new SqlCommand(sqlstr, sqlcon);
            sqlcmd.ExecuteNonQuery();

            sqlcon.Close();


            //************update 污染物********

            //********申报表里污染物（1）汞 到 （11）悬浮物*********
            //
            //污染物代码，按申报表里污染物名称前面的序号排列,记录(1)汞到(11)悬浮物
            //'020'汞  '022'镉   '024'六价铬  '027'铅 '026'砷  '110'挥发酚  '070' 氰化物 '011'化学需氧量  '080'石油类  '060' 氨氮 '003'悬浮物  
            string[] wrwdm = new string[] { "020", "022", "024", "027", "026", "110", "070", "011", "080", "060", "003" };

            HiddenField[] hb = new HiddenField[] { this.HiddenField1, this.HiddenField2, this.HiddenField3, this.HiddenField4, this.HiddenField5, this.HiddenField6, this.HiddenField7, this.HiddenField8, this.HiddenField9, this.HiddenField10, this.HiddenField11, this.HiddenField12, this.HiddenField13, this.HiddenField14, this.HiddenField15, this.HiddenField16 };
            TextBox[] tbxQCL = new TextBox[] { this.TextBoxQCL1, this.TextBoxQCL2, this.TextBoxQCL3, this.TextBoxQCL4, this.TextBoxQCL5, this.TextBoxQCL6, this.TextBoxQCL7, this.TextBoxQCL8, this.TextBoxQCL9, this.TextBoxQCL10, this.TextBoxQCL11, this.TextBoxQCL12, this.TextBoxQCL13, this.TextBoxQCL14, this.TextBoxQCL15, this.TextBoxQCL16 };
            TextBox[] tbxXZQCL = new TextBox[] { this.TextBoxXZQCL1, this.TextBoxXZQCL2, this.TextBoxXZQCL3, this.TextBoxXZQCL4, this.TextBoxXZQCL5, this.TextBoxXZQCL6, this.TextBoxXZQCL7, this.TextBoxXZQCL8, this.TextBoxXZQCL9, this.TextBoxXZQCL10, this.TextBoxXZQCL11, this.TextBoxXZQCL12, this.TextBoxXZQCL13, this.TextBoxXZQCL14, this.TextBoxXZQCL15, this.TextBoxXZQCL16 };
            TextBox[] tbxHJ = new TextBox[] { this.TextBoxHJ1, this.TextBoxHJ2, this.TextBoxHJ3, this.TextBoxHJ4, this.TextBoxHJ5, this.TextBoxHJ6, this.TextBoxHJ7, this.TextBoxHJ8, this.TextBoxHJ9, this.TextBoxHJ10, this.TextBoxHJ11, this.TextBoxHJ12, this.TextBoxHJ13, this.TextBoxHJ14, this.TextBoxHJ15, this.TextBoxHJ16 };
            TextBox[] tbxDBL = new TextBox[] { this.TextBoxDBL1, this.TextBoxDBL2, this.TextBoxDBL3, this.TextBoxDBL4, this.TextBoxDBL5, this.TextBoxDBL6, this.TextBoxDBL7, this.TextBoxDBL8, this.TextBoxDBL9, this.TextBoxDBL10, this.TextBoxDBL11, this.TextBoxDBL12, this.TextBoxDBL13, this.TextBoxDBL14, this.TextBoxDBL15, this.TextBoxDBL16 };
            TextBox[] tbxCBL = new TextBox[] { this.TextBoxCBL1, this.TextBoxCBL2, this.TextBoxCBL3, this.TextBoxCBL4, this.TextBoxCBL5, this.TextBoxCBL6, this.TextBoxCBL7, this.TextBoxCBL8, this.TextBoxCBL9, this.TextBoxCBL10, this.TextBoxCBL11, this.TextBoxCBL12, this.TextBoxCBL13, this.TextBoxCBL14, this.TextBoxCBL15, this.TextBoxCBL16 };

            for (int i = 0; i <= 10; i++)
            {
                if (hb[i].Value == "edit")
                {
                    sqlstr = "update a01107_2 set "
                    + "DEAL_COUNT=" + Convert.ToString(zh.StrToDouble(tbxQCL[i].Text.Trim()))
                    + ",NEW_NUM=" + Convert.ToString(zh.StrToDouble(tbxXZQCL[i].Text.Trim()))
                    + ",YEAR_SUMCOUNT=" + Convert.ToString(zh.StrToDouble(tbxDBL[i].Text.Trim()) + zh.StrToDouble(tbxCBL[i].Text.Trim()))
                    + ",REACH_COUNT=" + Convert.ToString(zh.StrToDouble(tbxDBL[i].Text.Trim()))
                    + ",OVER_COUNT=" + Convert.ToString(zh.StrToDouble(tbxCBL[i].Text.Trim()))
                    + " where SRKXH= " + Convert.ToString(i + 1)
                    + " and qiyeid=" + Session["qiyeid"] + " and  DATA_MONTH='" + Session["ShenBaoNianDu"] + "'";

                    sqlcon.Close();
                    sqlcmd.CommandText = sqlstr;
                    sqlcon.Open();
                    sqlcmd.ExecuteNonQuery();
                    tbxHJ[i].Text = Convert.ToString(zh.StrToDouble(tbxDBL[i].Text.Trim()) + zh.StrToDouble(tbxCBL[i].Text.Trim()));//解决提交页面刷新后变动回旧值得问题
                }
                else//新增
                {
                    if ((tbxQCL[i].Text.Trim() != "") || (tbxXZQCL[i].Text.Trim() != "") || (tbxDBL[i].Text.Trim() != "") || (tbxCBL[i].Text.Trim() != ""))
                    {
                        sqlstr = "insert into a01107_2(DEAL_COUNT,NEW_NUM,YEAR_SUMCOUNT,REACH_COUNT,OVER_COUNT,SRKXH,CODE,DATA_MONTH,qiyeid) values("
                         + Convert.ToString(zh.StrToDouble(tbxQCL[i].Text.Trim()))
                     + "," + Convert.ToString(zh.StrToDouble(tbxXZQCL[i].Text.Trim()))
                     + "," + Convert.ToString(zh.StrToDouble(tbxDBL[i].Text.Trim()) + zh.StrToDouble(tbxCBL[i].Text.Trim()))
                     + "," + Convert.ToString(zh.StrToDouble(tbxDBL[i].Text.Trim()))
                     + "," + Convert.ToString(zh.StrToDouble(tbxCBL[i].Text.Trim()))
                     + "," + Convert.ToString(i + 1) + ",'" + wrwdm[i] + "','" + Session["ShenBaoNianDu"] + " '," + Session["qiyeid"] + ")";

                        sqlcon.Close();
                        sqlcmd.CommandText = sqlstr;
                        sqlcon.Open();
                        sqlcmd.ExecuteNonQuery();

                        hb[i].Value = "edit";
                        tbxHJ[i].Text = Convert.ToString(zh.StrToDouble(tbxDBL[i].Text.Trim()) + zh.StrToDouble(tbxCBL[i].Text.Trim()));//解决提交页面刷新后变动回旧值得问题
                    }
                }
            }

            //*******污染物(11) 到 (16)*********用户自己选择污染物呢名称的
            DropDownList[] dpl = new DropDownList[] {this.DropDownList12 ,this.DropDownList13,this.DropDownList14,this.DropDownList15,this.DropDownList16};
            for (int i = 11; i <= 15; i++)
            {
                if (hb[i].Value == "edit")
                {
                    sqlstr = "update a01107_2 set "
                    + "DEAL_COUNT=" + Convert.ToString(zh.StrToDouble(tbxQCL[i].Text.Trim()))
                    + ",NEW_NUM=" + Convert.ToString(zh.StrToDouble(tbxXZQCL[i].Text.Trim()))
                    + ",YEAR_SUMCOUNT=" + Convert.ToString(zh.StrToDouble(tbxDBL[i].Text.Trim()) + zh.StrToDouble(tbxCBL[i].Text.Trim()))
                    + ",REACH_COUNT=" + Convert.ToString(zh.StrToDouble(tbxDBL[i].Text.Trim()))
                    + ",OVER_COUNT=" + Convert.ToString(zh.StrToDouble(tbxCBL[i].Text.Trim()))
                    + ",CODE='" + dpl[i-11].SelectedValue
                    + "' where SRKXH= " + Convert.ToString(i + 1)
                    + " and qiyeid=" + Session["qiyeid"] + " and  DATA_MONTH='" + Session["ShenBaoNianDu"] + "'";

                    sqlcon.Close();
                    sqlcmd.CommandText = sqlstr;
                    sqlcon.Open();
                    sqlcmd.ExecuteNonQuery();
                    tbxHJ[i].Text = Convert.ToString(zh.StrToDouble(tbxDBL[i].Text.Trim()) + zh.StrToDouble(tbxCBL[i].Text.Trim()));//解决提交页面刷新后变动回旧值得问题
                }
                else//新增
                {
                    if ((tbxQCL[i].Text.Trim() != "") || (tbxXZQCL[i].Text.Trim() != "") || (tbxDBL[i].Text.Trim() != "") || (tbxCBL[i].Text.Trim() != ""))
                    {
                        sqlstr = "insert into a01107_2(DEAL_COUNT,NEW_NUM,YEAR_SUMCOUNT,REACH_COUNT,OVER_COUNT,SRKXH,CODE,DATA_MONTH,qiyeid) values("
                         + Convert.ToString(zh.StrToDouble(tbxQCL[i].Text.Trim()))
                     + "," + Convert.ToString(zh.StrToDouble(tbxXZQCL[i].Text.Trim()))
                     + "," + Convert.ToString(zh.StrToDouble(tbxDBL[i].Text.Trim()) + zh.StrToDouble(tbxCBL[i].Text.Trim()))
                     + "," + Convert.ToString(zh.StrToDouble(tbxDBL[i].Text.Trim()))
                     + "," + Convert.ToString(zh.StrToDouble(tbxCBL[i].Text.Trim()))
                     + "," + Convert.ToString(i + 1) + ",'" + dpl[i-11].SelectedValue + "','" + Session["ShenBaoNianDu"] + " '," + Session["qiyeid"] + ")";

                        sqlcon.Close();
                        sqlcmd.CommandText = sqlstr;
                        sqlcon.Open();
                        sqlcmd.ExecuteNonQuery();

                        hb[i].Value = "edit";
                        tbxHJ[i].Text = Convert.ToString(zh.StrToDouble(tbxDBL[i].Text.Trim()) + zh.StrToDouble(tbxCBL[i].Text.Trim()));//解决提交页面刷新后变动回旧值得问题
                    }
                }
            }

            TextBoxWSPFL.Text = Convert.ToString(zh.StrToDouble(TextBoxCBPFL.Text.Trim()) + zh.StrToDouble(TextBoxDBPFL.Text.Trim()));//解决提交页面刷新后变动回旧值得问题

            sqlcon.Close();
            LabelTiShi.Visible = false;
            MessageBox("tishi", "你所填写申报内容正确，已成功提交!");
        }
    }


    //// <summary> 
    /// 弹出提示信息框 
    /// </summary> 
    /// <param name="strKey">函数key值</param> 
    /// <param name="strInfo">提示信息</param> 
    public void MessageBox(string strKey, string strInfo)
    {
        if (!ClientScript.IsClientScriptBlockRegistered(strKey))
        {
            string strjs = "alert('" + strInfo + "');";
            ClientScript.RegisterClientScriptBlock(this.GetType(), strKey, strjs, true);
        }
    } 
    
}